#!/usr/bin/python
import time

def setHoliday(list,year):
    year = str(year)
    holiday={'2021':[{'name':'元旦','startDay':'01-01','days':3,'workDays':[]},
    {'name':'春节','startDay':'02-11','days':7,'workDays':['02-07','02-20']},
    {'name':'清明节','startDay':'04-03','days':3,'workDays':[]},
    {'name':'劳动节','startDay':'05-01','days':5,'workDays':['04-25','05-08']},
    {'name':'端午节','startDay':'06-12','days':3,'workDays':[]},
    {'name':'中秋节','startDay':'09-19','days':3,'workDays':['09-18']},
    {'name':'国庆节','startDay':'10-01','days':7,'workDays':['09-26','10-09']}
    ]
    }
 
    for day in holiday[year]:
        startD = year+'-'+day['startDay']
        timestampSecond = time.mktime(time.strptime(startD,"%Y-%m-%d"))
        for d in range(day['days']):
            t = time.localtime(timestampSecond+24*3600*d)
            thisDate = time.strftime("%Y-%m-%d", t)
            for index in range(len(list)):
                if list[index]['day'] ==  thisDate :
                    l = list[index]
                    l['isWork'] = False
                    list[index] = l
                    break
        for workday in day['workDays']:
            thisdate = year+'-'+ workday
            for index in range(len(list)):
                if list[index]['day'] ==  thisdate :
                    l = list[index]
                    l['isWork'] = True
                    list[index] = l
                    break

def getDays(year,list,timestampSecond):
    t = time.localtime(timestampSecond)
    if(year == t.tm_year):
        isWork = t.tm_wday+1 != 6 and t.tm_wday+1 != 7
        thisDate = time.strftime("%Y-%m-%d", t)
        dict = {'day':thisDate,'dayOfWeek':t.tm_wday+1,'isWork':isWork,'timestamp':timestampSecond}
        list.append(dict)
        timestampSecond = timestampSecond +24*3600
        getDays(year,list,timestampSecond)

def getWorkDay(list):
    workdayList = []
    for day in list:
        if(day['isWork']):
            workdayList.append(day)
    return workdayList

def get(year):
    list = []
    timestampSecond = time.mktime(time.strptime(str(year)+"-01-01","%Y-%m-%d"))
    getDays(year,list,timestampSecond)
    setHoliday(list,year)
    list = getWorkDay(list)
    return list


def calculateWorkDay(year,startDay,days):
    list  = get(year)
    result = []
    thisDay = {}
    for day in list:
        if day['day'] == startDay:
            thisDay = day
            break
    startIndex = list.index(thisDay)
    endIndex  = startIndex + days 
    
    for i in range(startIndex,endIndex):
        result.append(list[i])
    return result




